package com.tandashi.springbootstudy01.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadLocalOfDataFormat {

    private static final ThreadLocal<SimpleDateFormat> tl = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    public static SimpleDateFormat getDateFormat(){
        SimpleDateFormat simpleDateFormat = tl.get();
        SimpleDateFormat simpleDateFormat1;
        if(simpleDateFormat == null){
            simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            tl.set(simpleDateFormat1);
            return simpleDateFormat1;

        }
        return simpleDateFormat;
    }

    public static String dateToString(Date date){
        if(date == null){
            return "";
        }
        return getDateFormat().format(date);
    }
}
